#include <iostream>
#include <QtGui>
#include "../item.h"
#include "../itemfactory.h"
#include "createitem.h"
#include "itemexist.h"
#include <string>

CreateItem::CreateItem(QWidget *parent) : QDialog(parent)
{
	// Initialize all variables
	id = new QLabel(tr("&Id:"));
	name = new QLabel(tr("&Name:"));
	desc = new QLabel(tr("&Description:"));
	pic = new QLabel(tr("&Picname:"));
	
	idEdit = new QLineEdit;
	nameEdit = new QLineEdit;
	descEdit = new QLineEdit;
	picEdit = new QLineEdit;

	id->setBuddy(idEdit);
	name->setBuddy(nameEdit);
	desc->setBuddy(descEdit);
	pic->setBuddy(picEdit);

	saveButton = new QPushButton(tr("&Save"));
	
	// Connect all the events to approprite slots
	connect(saveButton, SIGNAL(clicked()), this, SLOT(saveCurrent()));
	connect(idEdit, SIGNAL(textChanged(const QString &)), this, SLOT(setId(const QString &)));
	connect(nameEdit, SIGNAL(textChanged(const QString &)), this, SLOT(setName(const QString &)));
	connect(picEdit, SIGNAL(textChanged(const QString &)), this, SLOT(setPic(const QString &)));
	connect(descEdit, SIGNAL(textChanged(const QString &)), this, SLOT(setDesc(const QString &)));

	// Layout of the window
	QVBoxLayout *textLayout = new QVBoxLayout;
	textLayout->addWidget(id);
	textLayout->addWidget(name);
	textLayout->addWidget(desc);
	textLayout->addWidget(pic);
	QVBoxLayout *inputLayout = new QVBoxLayout;
	inputLayout->addWidget(idEdit);
	inputLayout->addWidget(nameEdit);
	inputLayout->addWidget(descEdit);
	inputLayout->addWidget(picEdit);
	QHBoxLayout *textInput = new QHBoxLayout;
	textInput->addLayout(textLayout);
	textInput->addLayout(inputLayout);
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addLayout(textInput);
	mainLayout->addWidget(saveButton);
	setLayout(mainLayout);

	setWindowTitle(tr("Create/Edit Item"));
	setFixedHeight(sizeHint().height());
}

// Save the changed/new item blueprint to disk
void CreateItem::saveCurrent(){
	
	try
	{
		// Create new blueprint item, make changes and save to disk
		ItemFactory *itemFactory = new ItemFactory();
		itemFactory->create(inputId);
		itemFactory->setName(inputName);
		itemFactory->setDescription(inputDesc);
		itemFactory->setPicName(inputPic);
		itemFactory->save();
		delete itemFactory;
	}
	catch(itemfactory_error &texterror)
	{
		if(overwrite())
		{
			try
			{
				// Load the item blueprint from disk, make changes and save
				ItemFactory *itemFactory = new ItemFactory();
				itemFactory->load(inputId);
				itemFactory->setName(inputName);
				itemFactory->setDescription(inputDesc);
				itemFactory->setPicName(inputPic);
				itemFactory->save();
				delete itemFactory;
				
			}
			catch(itemfactory_error &texterror)
			{
				loadError();
			}
			
		}
	}
}

// Save user input to Id
void CreateItem::setId(const QString & changedId)
{
	inputId = changedId.toStdString();
}

// Save user input to Name
void CreateItem::setName(const QString & changedId)
{
	inputName = changedId.toStdString();
}

// Save user input to Picture
void CreateItem::setPic(const QString & changedId)
{
	inputPic = changedId.toStdString();
}

// Save user input to Description
void CreateItem::setDesc(const QString & changedId)
{
	inputDesc = changedId.toStdString();
}

// Overwrite warning message
bool CreateItem::overwrite()
{
	int r = QMessageBox::warning(this, tr("Item"),
				     tr("This item already exists.\n"
					"Do you want to overwrite it?"),
				     QMessageBox::Yes | QMessageBox::Default,
				     QMessageBox::No);
	if (r == QMessageBox::Yes) {
		return true;
	} else if (r == QMessageBox::No) {
		return false;
	}
}

// Load warning message
void CreateItem::loadError()
{
	QMessageBox::warning(this, tr("Item"),
			     tr("Error while saving changes.\n"),
			     QMessageBox::Ok | QMessageBox::Default);
}
